package com.qingke.stockdata.service;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.qingke.stockdata.exception.InvalidStockCodeException;
import com.qingke.stockdata.model.Stock;
import com.qingke.stockdata.model.StockData;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class DataCrawlerImpl implements DataCrawler {

	
	//OKhttp

	public Stock getStock(String code) throws InvalidStockCodeException {
		OkHttpClient client = new  OkHttpClient();
		Stock stock = new Stock();
		
		Request request = new Request.Builder()
		      .url("http://hq.sinajs.cn/list=" + code)
		      .build();
		Response response;
		try {
			response = client.newCall(request).execute();
			String nowStockMessage = response.body().string();
			
			String[] tempArr = nowStockMessage.split("\"");
			String value = tempArr[1].trim();
			
			String[] tempArr1 = value.split(",");
			stock.setName(tempArr1[0]);
			stock.setCode(code);
			
//			for(int i =0;i<tempArr1.length;i++) {
//				System.out.println(tempArr1[i]);
//			}
//			System.out.println("=============");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return stock;
	}

	public List<StockData> getStockHist(String code) throws InvalidStockCodeException {
		OkHttpClient client = new  OkHttpClient();
		List<StockData> list = new ArrayList<StockData>();
		//if code 以sh开头要转换成ss
		String houzui = null;
		String shuzi = code.substring(2);
		if("sh".equals(code.substring(0, 2))) {
			 houzui ="ss";
		}else {
			houzui = code.substring(0, 2);
		}
//		System.out.println(houzui);
//		System.out.println(shuzi);
				
		
		Request request = new Request.Builder()
		      .url("http://table.finance.yahoo.com/table.csv?s=" + shuzi + "." + houzui )
		      .build();
		  Response response;
		try {
			response = client.newCall(request).execute();
			String stockHistoryMessage = response.body().string();
			String[] tempArr = stockHistoryMessage.split("\n");
			
			for(int i =1;i<tempArr.length;i++) {
				String[] tempArr1 = tempArr[i].split(",");
				StockData stockData = new StockData();
				stockData.setCode(code);
				SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" ); 
				stockData.setDate(sdf.parse(tempArr1[0]));
				stockData.setOpen(Double.parseDouble(tempArr1[1]));
				stockData.setHigh(Double.parseDouble(tempArr1[2]));
				stockData.setLow(Double.parseDouble(tempArr1[3]));
				stockData.setClose(Double.parseDouble(tempArr1[4]));
				stockData.setVolumn(Integer.parseInt(tempArr1[5]));
				stockData.setAdjclose(Double.parseDouble(tempArr1[6]));
				
				list.add(stockData);
				stockData.toString();
			}
			list.get(0).toString();
			
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
		
	}
//	public static void main(String[] arg){
//		DataCrawlerImpl dataCrawlerImpl = new DataCrawlerImpl();
//		try {
//			dataCrawlerImpl.getStock("sz000001");
//			List<StockData> lists = dataCrawlerImpl.getStockHist("sz000001");
//			for (StockData stockData : lists) {
//				System.out.println(stockData);
//			}
//		} catch (InvalidStockCodeException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//		
//	}
	
}
